package com.lovo.bbs.servlet.admin;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lovo.bbs.bo.LinkBo;
import com.lovo.bbs.vo.LinkVo;

public class LinkUpdateServlet extends HttpServlet{

	private static final long serialVersionUID = 7133064608524877572L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	       this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
	    int linkid = 0;//链接ID
	    if(request.getParameter("linkid") != null && !request.getParameter("linkid").equals(""))
	    	linkid=Integer.parseInt(request.getParameter("linkid"));
	    
	    String aslinkname = request.getParameter("linkname");
	    String linkname = null;//链接名
	    if(aslinkname!=null&&!"".equals(aslinkname)){
	    	linkname = new String(aslinkname.getBytes("ISO-8859-1"), "utf-8");
	    }
	    
	    String asaboutlink = request.getParameter("reads");
	    String aboutlink = null;//链接简介
	    if(asaboutlink!=null&&!"".equals(asaboutlink)){
	    	aboutlink = new String(asaboutlink.getBytes("ISO-8859-1"), "utf-8");
	    }
	    
	    String aslinkurl = request.getParameter("url");
	    String linkurl = null;//链接URL
	    if(aslinkurl!=null&&!"".equals(aslinkurl)){
	    	linkurl = new String(aslinkurl.getBytes("ISO-8859-1"), "utf-8");
	    }
	    
	    // 操作类型
		int opType = Integer.parseInt(request.getParameter("opType"));
		// 原页码,返回原页面时使用
		int queryPage = Integer.parseInt(request.getParameter("queryPage"));
		if (queryPage == 0) {
			queryPage = 1;
		}
	    
	    LinkVo vo = new LinkVo();
	    vo.setLinkid(linkid);
	    vo.setLinkname(linkname);
	    vo.setLinkurl(linkurl);
	    vo.setAboutLink(aboutlink);
	    
		int dataChanged = 0;
		ServletContext context = request.getSession().getServletContext();
		LinkBo linkBo = new LinkBo();
		if (opType == 1) {// 增加
			dataChanged = linkBo.updateLink(vo,LinkBo.OPER_OF_INSERT);
		} else if (opType == 2) {// 修改
			dataChanged = linkBo.updateLink(vo, LinkBo.OPER_OF_UPDATE);
		} else if (opType == 3) {//删除
			dataChanged = linkBo.updateLink(vo, LinkBo.OPER_OF_DELETE);
		}
		//更新内存中的链接表
		ArrayList<LinkVo> linkList = null;
		linkList = linkBo.getAllLink();
		context.setAttribute("linkList", linkList);

		if (dataChanged == 1) {
			resp
					.sendRedirect("friendForumQueryPage.jsp?queryPage="
							+ queryPage);
		}
	}

}
